# -*- coding: UTF-8 -*-

import gtk
from result import ResultWindow

class HistoryModel:
    def __init__(self, history):
        self.liststore = gtk.ListStore(str)
        self.history = history

    def get_model(self):
        if self.liststore:
            return self.liststore
        else:
            return None

    def appends(self):
        for item in self.history.getall():
            self.liststore.append([item.filename])

class HistoryView:
    def __init__(self, history, codes):
        self.history = history
        self.codes = codes

    def make(self, model):
        hbox = gtk.HBox()
        hbox.set_spacing(5)
        self.treeview = gtk.TreeView(model)
        self.treeview.connect('row-activated', self.doubleclick_cb)
        scrolled_window = gtk.ScrolledWindow()
        scrolled_window.add_with_viewport(self.treeview)
        scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
        scrolled_window.set_size_request(-1, 200)
        scrolled_window.show()
        self.cell = gtk.CellRendererText()
        self.tvcolumn = gtk.TreeViewColumn(_('Filename'), self.cell, text=0)
        self.tvcolumn.set_expand(True)
        self.tvcolumn.set_min_width(150)
        self.treeview.append_column(self.tvcolumn)
        hbox.pack_start(scrolled_window, True)
        self.treeview.show()
        hbox.show()
        return hbox
        
    def doubleclick_cb(self, treeview, iter, path, data=None):
        window = ResultWindow(self.codes)
        window.make(self.history.get(iter[0]))
        

class HistoryDialog:
    def __init__(self, history, codes):
        self.history = history
        self.codes = codes
        
    def make(self, parent=None):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title(_("History"))
        self.window.connect("destroy", self.__destroy_window)
        vbox = gtk.VBox()
        vbox.set_spacing(5)
        
        modelobj = HistoryModel(self.history)
        modelobj.appends()
        model = modelobj.get_model()
        
        viewobj = HistoryView(self.history, self.codes)
        view = viewobj.make(model)
        
        vbox.pack_start(view)
        
        hbox = gtk.HBox(spacing=5)
        delete_button = gtk.Button(stock=gtk.STOCK_DELETE)
        delete_button.connect("clicked", self._delete, viewobj)
        delete_button.show()
        clear_button = gtk.Button(stock=gtk.STOCK_CLEAR)
        clear_button.connect("clicked", self._clear, model)
        clear_button.show()
        hbox.pack_start(delete_button)
        hbox.pack_start (clear_button)
        hbox.show()
        vbox.pack_start(hbox)
        
        self.window.add(vbox)
        vbox.show()
        self.window.show()
        
    def __destroy_window(self, event):
        self.window.destroy()
        
    def _delete(self, widget, viewobj):
        model = viewobj.treeview.get_model()
        path, column = viewobj.treeview.get_cursor()
        if not path is None:
            iter = model.get_iter(path)
            self.history.remove(path[0])
            model.remove(iter)
        
    def _clear(self, widget, model):
        model.clear()
        self.history.clear()
